<?php

namespace app\admin\controller\prom;

use app\common\controller\Backend;
use think\Db;
/**
 * 砍价活动管理
 *
 * @icon fa fa-circle-o
 */
class Cutdown extends Backend
{
    
    /**
     * Cutdown模型对象
     * @var \app\admin\model\Cutdown
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Cutdown');
        $this->view->assign("statusList", $this->model->getStatusList());
        $this->view->assign("IsEndList", $this->model->getIsEndList());


    }

    /**
     * 编辑
     */
    public function edit($ids = NULL)
    {
        $row = $this->model->get($ids);
        if (!$row)
            $this->error(__('No Results were found'));
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds))
        {
            if (!in_array($row[$this->dataLimitField], $adminIds))
            {
                $this->error(__('You have no permission'));
            }
        }
        if ($this->request->isPost())
        {
            $params = $this->request->post("row/a");
            if ($params)
            {
                try
                {
                    //是否采用模型验证
                    if ($this->modelValidate)
                    {
                        $name = basename(str_replace('\\', '/', get_class($this->model)));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
                        $row->validate($validate);
                    }
                    $result = $row->allowField(true)->save($params);
                    if ($result !== false)
                    {
                        $this->success();
                    }
                    else
                    {
                        $this->error($row->getError());
                    }
                }
                catch (\think\exception\PDOException $e)
                {
                    $this->error($e->getMessage());
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $goodsAttrData = Db::name('goods_attr')->where(['goods_id' => $row['goods_id']])->select();
        $this->view->assign('goodsAttrData',$goodsAttrData);
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

    public function getAttrByGoodsId(){
        $goods_id = $this->request->request('goods_id');
        $data = Db::name('goods_attr')->where(['goods_id' => $goods_id])->select();

        return json(['code' => 1,'msg'=>'','data' =>$data]);
    }


    

}
